package com.css.fxfzcpxx.modules.certificateManage.controller;

import com.css.fxfzcpxx.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzcpxx.base.response.RestResponse;
import com.css.fxfzcpxx.constants.YNEnum;
import com.css.fxfzcpxx.modules.certificateManage.entity.CertificateManage;
import com.css.fxfzcpxx.modules.certificateManage.entity.CertificateRule;
import com.css.fxfzcpxx.modules.certificateManage.entity.UserCertificate;
import com.css.fxfzcpxx.modules.certificateManage.service.CertificateManageService;
import com.css.fxfzcpxx.modules.certificateManage.service.impl.CertificateManageServiceImpl;
import com.css.fxfzcpxx.modules.certificateManage.vo.CertificateRuleVo;
import com.css.fxfzcpxx.modules.certificateManage.vo.CertificateVo;
import com.css.fxfzcpxx.modules.courseManage.param.CourseManageParam;
import com.css.fxfzcpxx.modules.courseManage.param.CourseManageServiceParam;
import com.css.fxfzcpxx.modules.courseManage.service.CourseManageService;
import com.css.fxfzcpxx.modules.thematicMap.vo.ThematicMapVo;
import com.css.fxfzcpxx.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: 徐治宇
 * @CreateTime: 2023/03/13
 * @Description: 证书管理
 */
@Api(tags ="证书管理")
@Slf4j
@RestController
@RequestMapping("/certificateManage")
public class CertificateManageController {
    @Autowired
    CertificateManageService certificateManageService;

    /**
     * 证书预览
     * @param fileName
     * @param certificateVo
     * @return
     */
    @PostMapping("/preview/{fileName}")
    public RestResponse preview(@PathVariable("fileName") String fileName,@RequestBody CertificateVo certificateVo){
        RestResponse restResponse = null;
        try {
            AttachmentInfoEntity atts=certificateManageService.preview(fileName,certificateVo);
            restResponse = RestResponse.succeed(atts);
        } catch (Exception e) {
            String errorMessage = "预览失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 删除模板
     * @param id 模板id
     * @return
     */
    @PostMapping("/previewDel/{id}")
    public RestResponse previewDel( @PathVariable("id")String id){
        RestResponse restResponse = null;
        try {
            return certificateManageService.previewDel(id);
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 获取证书附件
     * @return
     */
    @GetMapping("/getCertificateFile")
    public RestResponse getGeneratedCertificate(){
        RestResponse restResponse = null;
        try {
            List<AttachmentInfoEntity> list=certificateManageService.getCertificateFile();
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    @GetMapping("/queryAll")
    public RestResponse queryAll(){
        RestResponse restResponse = null;
        try {
            List<CertificateManage> list=certificateManageService.queryAll();
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 添加模板
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody  CertificateManage certificateManage){
        RestResponse restResponse = null;
        try {
            restResponse=certificateManageService.save(certificateManage);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 修改
     * @return
     */
    @PostMapping("/update")
    public RestResponse certificateSave(@RequestBody CertificateManage certificateManage){
        RestResponse restResponse = null;
        try {
            certificateManageService.update(certificateManage);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 修改规则
     * @return
     */
    @PostMapping("/updateRule")
    public RestResponse updateRule(@RequestBody List<CertificateRule> ruleList){
        RestResponse restResponse = null;
        try {
            certificateManageService.updateRule(ruleList);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    @GetMapping("/queryRule")
    public RestResponse queryRule(){
        RestResponse restResponse = null;
        try {
            List<CertificateRuleVo> list=certificateManageService.queryRule();
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    //获取当前用户证书
    @GetMapping("/getUserCertificate")
    public RestResponse getUserCertificate(){
        RestResponse restResponse = null;
        try {
            List<UserCertificate> userCertificate=certificateManageService.getUserCertificate();
            restResponse = RestResponse.succeed(userCertificate);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }
}
